package models;
import java.util.Date;  
import java.util.List;
import java.util.ArrayList;
import javax.persistence.*;  
import play.db.jpa.Model;  

@Entity
public class User extends Model{
    public String name; 
    public String password;
    public String avatar = "/public/images/man.GIF"; //默认头像文件的地址
    public String background; //背景图片
    public  Integer  postCnt = 0;
    public  Integer  followersCnt = 0; //粉丝数
    public  Integer  followingCnt = 0; //关注数
    public String weiboToken;
    
    
    public User(String name, String password){
        this.name = name;
        this.password = password;
    }
    public void follow(User usr){
        new Follow(this,usr).save();
    }
    //获取粉丝
    public List<User> getFollowers(){
        List<Follow> relations = Follow.findAll();
        List<User> fans = new ArrayList<User>();
        for(Follow follow:relations){
                if(follow.toWho.equals(this) ){
                       fans.add(follow.fromWho);
                }
        }
        return fans;

    } 
    //获取所关注用户
    public List<User> getFollowing(){
        List<Follow> relations = Follow.findAll();
        List<User> pubs = new ArrayList<User>();
        for(Follow follow:relations){
                if(follow.fromWho.equals(this)){
                       pubs.add(follow.toWho);
                }
        }
        return pubs;
    }
    
    public List<MBlog> getMBlog(){
        List<MBlog> post = MBlog.findAll();
        List<MBlog> myMBlog = new ArrayList<MBlog>();
        //List<User> usr = new ArrayList<User>();
        for(MBlog p:post){
                if(p.author.equals(this)){
                       myMBlog.add(p);
                }
        }
        return myMBlog;
    }

    //取得自己的所有聊天消息
    public List<Message> getMessages(){
        return Message.find("byReciever",this).fetch();
    }
   
    //@到该用户的微博 
    public List<MBlog> getAtMe(){
        List<At> ats = At.find("byUser",this).fetch();
        List<MBlog> posts = new ArrayList<MBlog>();
        for(At at:ats){
                posts.add(at.post);
        }
        return posts;
    }

    public void sendMessage(User usr){
        
    }
    public void commend(MBlog post){

    }

}
